<?php
/* required libraries */
require('./library/strings.php');
require('./library/classes.php');

/* create the entry page and display it */
$entry = new CABLEentry();
echo $entry->source;

/* classes */
class CABLEentry
{
	public	$source;
	public	$config;
	public	$db;
	public	$user;
	
	private	$cache;
	private	$plugins	= array();
	private	$parsers	= array();
	private	$template	= false;
	private	$messages	= array();	
	private	$error		= false;
	
	public	function __construct($timestamp=false)
	{
		$this->error = true;
		$this->config = new CABLEconfig();
		if ($this->config->error === false)
		{
			$this->db = new CABLEdb();
			if ($this->db->Connect($this->config->values['DbServer'],$this->config->values['DbUsername'],$this->config->values['DbPassword'],$this->config->values['DbName']))
			{
				$this->user	= new CABLEuser($this->config,$this->db);
				if ($this->user->error === false)
					$this->error = false;
			}
		}
				
		/* if the constructor been passed a timestamp, replace the default from CABLEuser */
		if ($timestamp)
			$this->user->timestamp = $timestamp;

		/* check the cache, create new if necessary */
		if ($this->error === false)
		{
			/* initialize cache */
			$this->cache = new CABLEcache('entry',$this->config);
			
			/* see if there's a template in the cache */
			if (($cache = $this->cache->GetCache($this->user->timestamp)) === false)
			{
				/* build the entry from scratch */
				$this->source = $this->BuildEntry(true);
				
				/* write the static template contents to the cache */
				$this->cache->WriteCache($this->user->timestamp,$this->template->cache);
			}
			else
			{
				$this->template = new CABLEtemplate($cache);
				$this->source = $this->BuildEntry(false);
			}
			
			if ($this->source === false)
				$this->error = true;
		}

		/* message collection */
		if (($this->config->values['ErrorThreshold'] > 0) || ($this->error !== false))
		{
			$this->messages = CABLE_MessageArrayCombine($this->messages,$this->config->messages);
			$this->messages = CABLE_MessageArrayCombine($this->messages,$this->db->messages);
			$this->messages = CABLE_MessageArrayCombine($this->messages,$this->user->messages);
			$this->messages = CABLE_MessageArrayCombine($this->messages,$this->cache->messages);
			$this->messages = CABLE_MessageArrayCombine($this->messages,$this->template->messages);
				
			$this->source = CABLE_MakeMessages($this->messages,$this->config->values['ErrorThreshold']).$this->source;
		}
		
		return null;
	}
		
	private	function BuildEntry($new)
	{
		if ($new) /* if new, we'll need to build the static template elements */
		{
			/* retrieve base template from database */
			$this->template = new CABLEtemplate($this->config,$this->db,$this->config->values['ThemeName'],'entry');
			if ($this->template->error)
				return false;
		
			/* retrieve entry data from database */
			$table = "cable_".$this->config->values['TablePrefix']."_entries";
			$query = "SELECT * FROM $table WHERE ((timestamp = '".$this->user->timestamp."') AND (visible=1)) LIMIT 1";
			if(($result = mysql_query($query,$this->db->db_link)) === false)
			{
				$this->messages[0][] = str_replace('<id>',mysql_error($this->db->db_link),CABLE_ERROR_DATABASE_REQUEST);
				return false;
			}
			
			if (mysql_num_rows($result) == 0) /* no entry for that timestamp */
				$this->template->SetBool('Exists',false);
			else
				$this->template->SetBool('Exists',true);
		
			/* retrieve entry data from the result */
			$entry_data = mysql_fetch_array($result,MYSQL_ASSOC);
			
			/* extract serialized entry data */
			$data = unserialize($entry_data[data]);
			foreach($data as $key => $value)
			{
				if (!isset($entry_data[$key]))
					$entry_data[$key] = $value;
			}
			unset($entry_data[data]);
			
			/* undecode HTML special chars */
			$entry_data[title] = rawurldecode($entry_data[title]);
			/* break html into singular lines */
			$entry_data[lines] = explode("\n",stripslashes(rawurldecode($entry_data[text])));
		}
		else
			$entry_data = array('cached'=>true);
		
		/* load plugins and run initialization functions */
		$plugin_names = explode(',',$this->config->values['EntryPlugins']);
		foreach($plugin_names as $name)
		{
			$this->plugins[$name] = new CABLEplugin($name,$this->config,$this->db,$this->user);
			$this->plugins[$name]->Connect($this->template,$entry_data);
			$this->plugins[$name]->Execute('open');
			
			/* consolidate plugin error messages */
			$this->messages = CABLE_MessageArrayCombine($this->messages,$this->parsers[$name]->messages);
			unset($this->parsers[$name]->messages);
			
			/* bail if necessary */
			if ($this->plugins[$name]->abort)
				break;
		}
		
		/* parsers only need to be run if data isn't being cached */
		if ($new)
			$parser_names = explode(',',$this->config->values['EntryParsers']);
		else
			$parser_names = array();

		/* run entry lines through parsers */
		foreach($parser_names as $name)
		{
			$this->parsers[$name] = new CABLEplugin($name,$this->config,$this->db,$this->user);
			$this->parsers[$name]->Connect($this->template,$entry_data);
			
			/* run parser initialization/opening functions */
			$this->parsers[$name]->Execute('open');
			
			/* run parser default routine for each entry line */
			for($this->parsers[$name]->connector->data[line_counter] = 0; $this->parsers[$name]->connector->data[line_counter] < count($entry_data[lines]); $this->parsers[$name]->connector->data[line_counter]++)
				$this->parsers[$name]->Execute('');
			
			/* run parser closing functions */
			$this->parsers[$name]->Execute('close');
			
			/* consolidate plugin error messages */
			$this->messages = CABLE_MessageArrayCombine($this->messages,$this->parsers[$name]->messages);
			unset($this->parsers[$name]->messages);
			
			/* bail if necessary */
			if ($this->parser[$name]->abort)
				break;
			
			/* clean up */
			unset($this->parsers[$name]);
		}
		
		/* run plugin main functions */
		foreach($plugin_names as $name)
		{
			$this->plugins[$name]->Connect($this->template,$entry_data);
			$this->plugins[$name]->Execute('');
			
			/* retrieve messages */
			$this->messages = CABLE_MessageArrayCombine($this->messages,$this->plugins[$name]->messages);
			unset($this->plugins[$name]->messages);
			
			/* bail if necessary */
			if ($this->plugins[$name]->abort)
				break;
		}
		
		/* run plugin closing functions */
		foreach($plugin_names as $name)
		{
			$this->plugins[$name]->Connect($this->template,$entry_data);
			$this->plugins[$name]->Execute('close');
			
			/* retrieve messages */
			$this->messages = CABLE_MessageArrayCombine($this->messages,$this->plugins[$name]->messages);
			unset($this->plugins[$name]->messages);
			
			/* bail if necessary */
			if ($this->plugins[$name]->abort)
				break;
			
			/* clean up */
			unset($this->plugins[$name]);
		}
				
		return $this->template->Render();
	}
}

?>